$(function(){


    var address = null;
    $.ajax({
        type: "get",
        url: "/address/queryAddress",
        success: function (res) {
            console.log(res)
            address = res;
        var html = template("addressTpl",{result:res});
        $("#address-box").html(html);
        }
    });


    $("#address-box").on("tap",".delete-btn",function(){
        var id = this.getAttribute("data-id");
        var li = this.parentNode.parentNode;

        mui.confirm("确定要删除吗？",function(mes){
            if(mes.index == 1){
                $.ajax({
                    type: "post",
                    url: "/address/deleteAddress",
                    data: {
                        id : id
                    },
                    success: function (res) {
                        if(res.success){
                            location.reload();
                        }
                    }
                });
            }else{
                mui.swipeoutClose(li);
            }
        });
    });

    $("#address-box").on("tap",".edit-btn",function(){
        var id = $(this).attr("data-id");
        console.log(address);
        for(var i=0;i<address.length;i++){
            if(address[i].id == id){
             localStorage.setItem('editAddress',JSON.stringify(address[i])); 
             break;
            }
            
        }
        location.href = "addAddress.html?isEdit=1";
    });

});